unit LoggerFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, AppConst;

type
  TLoggerForm = class(TForm)
    pnlBottom: TPanel;
    btnOK: TButton;
    pnlClient: TPanel;
    pbBar: TProgressBar;
    mmoInfo: TMemo;
    btnAbort: TButton;
    procedure FormShow(Sender: TObject);
    procedure btnAbortClick(Sender: TObject);
  private
    FAutoRun: Boolean;
    FOnAbort: TNotifyEvent;
    FIsMultiThread: Boolean;
    procedure SetAutoRun(const Value: Boolean);
  public
    constructor Create(AOwner: TComponent; AIsMultiThread: Boolean = True); reintroduce;
    procedure BeginLog;
    procedure EndLog;

    procedure AddInfo(const AInfo: string);
    procedure AddBlankInfo;

    procedure SetProcessBarRange(AMin, AMax: Integer);
    procedure SetProcessBarPos(APos: Integer);

    property AutoRun: Boolean read FAutoRun write SetAutoRun;
    property OnAbort: TNotifyEvent read FOnAbort write FOnAbort;
  end;

var
  LoggerForm: TLoggerForm;

const
  CM_9527 = 9527;
  CM_9528 = 9528;

implementation

{$R *.dfm}

{ TLoggerForm }

procedure TLoggerForm.AddBlankInfo;
begin
  AddInfo('');
end;

procedure TLoggerForm.AddInfo(const AInfo: string);
begin
  mmoInfo.Lines.Add(AInfo);
end;

procedure TLoggerForm.BeginLog;
begin
  mmoInfo.Lines.Clear;
  btnOK.Enabled := False;
  btnAbort.Enabled := True;
end;

procedure TLoggerForm.btnAbortClick(Sender: TObject);
begin
  if Assigned(OnAbort) then OnAbort(Self);  
end;

constructor TLoggerForm.Create(AOwner: TComponent; AIsMultiThread: Boolean);
begin
  inherited Create(AOwner);
  FIsMultiThread := AIsMultiThread;
end;

procedure TLoggerForm.EndLog;
begin
  btnOK.Enabled := True;
  btnOK.SetFocus;
  btnAbort.Enabled := False;

  if AutoRun then ModalResult := mrOk;
end;

procedure TLoggerForm.FormShow(Sender: TObject);
begin
  if FIsMultiThread then
    SendMessage(Application.MainFormHandle, CM_9527, Integer(Self), 0);
end;

procedure TLoggerForm.SetAutoRun(const Value: Boolean);
begin
  FAutoRun := Value;
end;

procedure TLoggerForm.SetProcessBarPos(APos: Integer);
begin
  pbBar.Position := APos;
end;

procedure TLoggerForm.SetProcessBarRange(AMin, AMax: Integer);
begin
  pbBar.Max := AMax;
  pbBar.Min := AMin;
end;

end.
